import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
import { clearUserLocalStorage } from "../../../../utils/util";
const app = getApp();
Page({
    data: {
        order: undefined,
        mergeOrders: [],
        remark: '',
        sum_price: 0, //计算订单总价
        button_status: 1, //点击提交按钮禁用


        orderAll: [], //订单信息
        o_id: 0, //默认订单id
        fileList: [], //图片数组
        imgMax: 1048576 * 2, //单图片大小
        activeNames: ['0'],

        driver: [], //登录人员信息

        controlUpload: true, //控制文件上传
        imgDelete: true, //控制文件删除


        view_delivery_man_id: 0,
        view_driver_name: '',
        view_delivery_district_id: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const that = this
        //如果登录缓存过期就跳转登录页面
        let now = (new Date().getTime()) / 1000;
        let expiration_time = wx.getStorageSync('expiration_time')
        let group = wx.getStorageSync('role')

        let current_delivery_man_id = wx.getStorageSync('current_delivery_man_id')
        if (!current_delivery_man_id || current_delivery_man_id == 0) {
            current_delivery_man_id = 0
        }

        if (group !== '配送司机') {
            clearUserLocalStorage()
            expiration_time = 0
        }


        /*
        if (now > expiration_time) {
            wx.reLaunch({
                url: '/pages/login/login',
            })
        }*/

        let access_token = wx.getStorageSync('access_token')
        let delivery_man = wx.getStorageSync('delivery_man')
        let delivery_district = wx.getStorageSync('delivery_district')
        this.setData({
            'access_token': access_token,
            'delivery_man': delivery_man,
            'delivery_district': delivery_district,
            'level': delivery_man.level,
            'current_delivery_man_id': current_delivery_man_id
        })

        //接收上级传递的参数
        const eventChannel = this.getOpenerEventChannel() //使用const 声明的不能被修改，必须立刻赋值
        eventChannel.on('response', function (res) {
            console.log('res:', res)
            let response = JSON.parse(res)  //传值过来是json 需要转换对象
            console.log('res:', response)
            let order = response.order
            let mergeOrders = response.mergeOrders
            let sum_price = 0
            let remark = ''
            console.log(order)
            console.log(mergeOrders)

            for (let i = 0; i < mergeOrders.length; i++) {
                sum_price += parseFloat(mergeOrders[i]['deliver_amount'])
                let customer_remark = mergeOrders[i]['customer_remark']
                if (customer_remark.length > 0) {
                    remark += customer_remark + '\n'
                }
                console.log('sum_price:', sum_price, remark)
            }

            that.setData({
                order,
                mergeOrders,
                sum_price,
                remark
            })
        })

    },

    async bindingOrder(event) {
        const that = this
        this.setData({
            button_status: 0, //点击绑定，按钮不能点击
        })

        let remark = this.data.remark
        if (remark.length > 0) {
            await Dialog.alert({
                title: '请注意商户留言:',
                message: remark,
                showConfirmButton: true,
            }).then(() => {
                // on close
            });
        }

        let order_id = that.data.order.order_id
        let current_delivery_man_id = that.data.current_delivery_man_id
        if (current_delivery_man_id === 0) {
            current_delivery_man_id = that.data.delivery_man.delivery_man_id
        }
        wx.request({
            url: app.globalData.API + '/delivery/index/binding-order',
            header: {
                'Content-Type': 'application/json',
                "x-api-key": this.data.access_token,
            },
            method: "POST",
            data: {
                order_id,
                current_delivery_man_id
            },
            success: (res) => {
                if (res.data.code === 200) {
                    let response = res.data.data
                    Toast('绑定成功~~')
                    wx.reLaunch({
                        url: '/templates/deliver/pages/index/index',
                    })
                } else {
                    Toast('绑定失败~~')
                    wx.reLaunch({
                        url: '/templates/deliver/pages/index/index',
                    })
                }
            }
        })
    },
    /**
     * 默认打开折叠
     */
    onChange(event) {
        console.log(event.detail)
        this.setData({
            activeNames: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})